/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import hibernate.model.dao.KhachHangDAO;
import hibernate.pojo.Khachhang;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Administrator
 */
@WebServlet(name = "updatingcustomerinfocontroller", urlPatterns = {"/updatingcustomerinfocontroller"})
public class updatingcustomerinfocontroller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try
        {
            HttpSession session = request.getSession();
            request.setCharacterEncoding("UTF-8");
            String action = request.getParameter("action");
            if(action.equals("changepassword"))
            {
                request.getRequestDispatcher("pagestile/changepassword.jsp").forward(request, response);
            }
            if(action.equals("updatingaccount"))
            {
                request.getRequestDispatcher("pagestile/updatingcustomerinfo.jsp").forward(request, response);
            }
            if(action.equals("update"))
            {
                Khachhang khachHang = (Khachhang) session.getAttribute("KhachHang");
                String matKhauHienTai = request.getParameter("matkhauhientai");
                String maHoaMatKhauHienTai = KhachHangDAO.encryptPassword(matKhauHienTai);
                if(!maHoaMatKhauHienTai.equals(khachHang.getMatKhau()))
                {
                    request.getRequestDispatcher("pagestile/updatingcustomerinfo.jsp").forward(request, response);
                }
                String hoTen = request.getParameter("hoten");
                String gioiTinh =  request.getParameter("gioitinh");
                if(gioiTinh.equals("0"))
                {
                    gioiTinh = "Nữ";
                }
                else
                {
                    gioiTinh = "Nam";
                }
                String ngay = request.getParameter("ngay");
                String thang = request.getParameter("thang");
                String nam = request.getParameter("nam");
                
                String strNgaySinh = nam + "-" + thang + "-" + ngay;
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
                Date ngaySinh = new Date();
                try 
                {
                    ngaySinh = sdf.parse(strNgaySinh);
                } catch (ParseException ex) {
                    Logger.getLogger(updatingcustomerinfocontroller.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                String diaChi =  request.getParameter("diachi"); 
                String thanhPho =  request.getParameter("tinhthanhpho"); 
                String soDienThoai =  request.getParameter("sodienthoai"); 
                String tinhTrangHonNhan =  request.getParameter("tinhtranghonnhan"); 
                String ngheNghiep =  request.getParameter("nghenghiep"); 
                String cmnd =  request.getParameter("cmnd");
                
                String ngayCap = request.getParameter("ngayCap");
                String thangCap = request.getParameter("thangCap");
                String namCap = request.getParameter("namCap");
                
                String strNgayCap = namCap + "-" + thangCap + "-" + ngayCap;
                Date dCap = new Date();
                try 
                {
                    dCap = sdf.parse(strNgayCap);
                } catch (ParseException ex) {
                    Logger.getLogger(updatingcustomerinfocontroller.class.getName()).log(Level.SEVERE, null, ex);
                }
                 
                String noiCap =  request.getParameter("noicap"); 
                String email = request.getParameter("email"); 
                String cauHoi = request.getParameter("cauhoi"); 
                
                khachHang.setHoTen(hoTen);
                khachHang.setGioiTinh(gioiTinh);
                khachHang.setNgaySinh(ngaySinh);
                khachHang.setDiaChi(diaChi);
                khachHang.setThanhPho(thanhPho);
                khachHang.setSoDienThoai(soDienThoai);
                khachHang.setTinhTrangHonNhan(tinhTrangHonNhan);
                khachHang.setNgheNghiep(ngheNghiep);
                khachHang.setCmnd(cmnd);
                khachHang.setEmail(email);
                khachHang.setNgayCap(dCap);
                khachHang.setNoiCap(noiCap);
                khachHang.setCauHoiBiMat(cauHoi);
                boolean update = KhachHangDAO.updateCustomerInfo(khachHang);
                if(update==true)
                {
                    // Hình như là không setAttribute lại được cho khách hàng đã có
                    response.sendRedirect("customerinfocontroller?action=viewinfo");
                }
                else
                {
                    request.getRequestDispatcher("pagestile/updatingcustomerinfo.jsp").forward(request, response);
                }
            }
            
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
